package com.accounting.juanjuan.controller;

import com.accounting.juanjuan.entity.ResultData;
import com.accounting.juanjuan.service.AccountingService;
import com.accounting.juanjuan.utils.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类
 *
 * @Author xxj
 * @Date 2024/1/27 22:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/accounting")
public class AccountingController {
    private final AccountingService accountingService;

    public AccountingController(AccountingService accountingService) {
        this.accountingService = accountingService;
    }

    /**
     * 根据文件获取标题
     */
    @PostMapping("/getTitle")
    public List<String> getTitle(@RequestPart("file") MultipartFile file, @RequestParam("sheetName") String sheetName) {
        Assert.isExcelFile(file);
        return accountingService.getExcelTitle(file, sheetName);
    }

    /**
     * 获取计算后的数据
     */
    @PostMapping("/getComputeData")
    public ResultData getComputeData(@RequestPart("file") MultipartFile file, @RequestParam("sheetName") String sheetName, @RequestParam("columnName") String columnName, @RequestParam("enforcement") String enforcement) {
        Assert.isExcelFile(file);
        Assert.isNotBlank(columnName, "文件指定列名字");
        Assert.isNotBlank(enforcement, "执行力度");
        return accountingService.getComputeAfterData(file, sheetName, columnName, enforcement);
    }

    /**
     * 根据内容生成excel文件，并下载
     */
    @PostMapping("/downExcelFile")
    public void downExcelFile(HttpServletResponse response, @RequestParam("fileName") String fileName, @RequestParam("columnName") String columnName, @RequestParam("data") String data) {
        Assert.isNotBlank(fileName, "文件名");
        Assert.isNotBlank(columnName, "文件指定列名字");
        Assert.isNotBlank(data, "生成的数据");
        accountingService.downExcelFileOnData(response, fileName, columnName, data);
    }
}
